/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "imgops/skin/SkinRecNBayesOp.h"
#include "common/utility_data_structures.h"
#include "factory/ResourceGeneratorFactory.h"

#include <map>
#include <string>

using namespace std;
using namespace cv;

namespace NAMESPACE{

// ****** Functor implementation ***********************************************
bool SkinRecNBayesFunc::operator()(const cv::Mat& src,
                                   const NormalBayesModel& model,
                                   cv::Mat& out_mask,
                                   CVT_SPACE cvt_space,
                                   int _class){
    skin_recognition_nbayes(src, model, out_mask, cvt_space, _class);
    return true;
}
// -----------------------------------------------------------------------------

// ****** Constants assignment *************************************************
const size_t SkinRecNBayesOp::MIN_INPUT_NUM = 2;
const size_t SkinRecNBayesOp::MIN_CRITERIA_NUM = 1;
const size_t SkinRecNBayesOp::MIN_OUTPUT_NUM = 1;
// -----------------------------------------------------------------------------

// ****** Operation implementation *********************************************
SkinRecNBayesOp::~SkinRecNBayesOp(){}
SkinRecNBayesOp::SkinRecNBayesOp() : AbstractOperation<SkinRecNBayesFunc>(){

    min_input_num = MIN_INPUT_NUM;
    min_criteria_num = MIN_CRITERIA_NUM;
    min_output_num = MIN_OUTPUT_NUM;

    k1.set_key(INPUT_NS, "img"); k1.setType(TypeParseTraits<cv::Mat*>::name());
    k2.set_key(INPUT_NS,"model.stat.bayes"); k2.setType(TypeParseTraits<NormalBayesModel*>::name());

    k3.set_key(OUTPUT_NS, "mask"); k3.setType(TypeParseTraits<cv::Mat*>::name());

    k4.set_key(CRITERIA_NS, "constant.1"); k4.setType(TypeParseTraits<CVT_SPACE*>::name());
    k5.set_key(CRITERIA_NS, "value.class"); k5.setType(TypeParseTraits<int>::name());

    input_keys->push_back(k1);
    input_keys->push_back(k2);

    output_keys->push_back(k3);

    criteria_keys->push_back(k4);
    criteria_keys->push_back(k5);
}
SkinRecNBayesOp::SkinRecNBayesOp(const SkinRecNBayesOp& gbo) : AbstractOperation<SkinRecNBayesFunc>(gbo){}
SkinRecNBayesOp& SkinRecNBayesOp::operator=(const SkinRecNBayesOp& gbo){
    if(this == &gbo) return *this;
    AbstractOperation<SkinRecNBayesFunc>::operator=(gbo);
    return *this;
}

bool SkinRecNBayesOp::doOperation(const std::list<input_t>& inputs,
                                    const std::list<criteria_t>& operationCriteria,
                                    std::list<output_t>& outputs) throw(bad_io_mapping){

    AbstractOperation<SkinRecNBayesFunc>::prepareIO(inputs, operationCriteria, outputs);

    Mat* in_img  = boost::any_cast<Mat*>( ((*input_mapping) [k1])->getElement() );
    if (in_img->empty()) return false;
    NormalBayesModel* model = boost::any_cast<NormalBayesModel*>( ((*input_mapping)[k2])->getElement() );

    Mat* out_mask_img = boost::any_cast<Mat*>( ((*output_mapping)[k3])->getElement() );

    CVT_SPACE* cvt_space  = boost::any_cast<CVT_SPACE*>( ((*criteria_mapping)[k4])->getElement() );

    // Optional parameters
    IOElement* io_class = (*criteria_mapping)[k5];
    int _skin = 1;
    int* _class = io_class ? boost::any_cast<int*>( io_class->getElement() ) : &_skin;

    (*operation)(*in_img, 
                 *model,
                 *out_mask_img,
                 *cvt_space,
                 *_class);

    return true;
}

bool SkinRecNBayesOp::operator==(const AbstractOperation<operation_t>& op) const{
    return AbstractOperation<SkinRecNBayesFunc>::operator==(op);
}

bool SkinRecNBayesOp::operator!=(const AbstractOperation<operation_t>& op) const{
    return !operator==(op);
}
// -----------------------------------------------------------------------------

REGISTER_PARSE_TYPE(SkinRecNBayesOp);
SimpleFactoryImpl<PipeStepComputation, SkinRecNBayesOp> skinRecNBayesOpFactory_instance;
}
